package com.compomics.proteocloud.s3;

import java.io.File;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;

import org.jets3t.service.S3ServiceException;
import org.jets3t.service.impl.rest.httpclient.RestS3Service;
import org.jets3t.service.model.S3Bucket;
import org.jets3t.service.model.S3Object;
import org.jets3t.service.security.AWSCredentials;

/**
 * S3Manager features bucket handling, downloading and uploading of files to the S3. 
 * @author Thilo Muth
 *
 */
public class S3Manager {
	/**
	 * The S3Service object.
	 */
	private RestS3Service s3Service;
	
	/** 
	 * The AWS Credentials object.
	 * @param awsCredentials
	 */
	private AWSCredentials awsCredentials;

	/**
	 * The bucket's name.
	 */
	private String bucketname;
	
	/**
	 * Default download folder on instance.
	 */
	private final static String DATASET_PATH = "/home/ubuntu/datasets/";
	
	/**
	 * Constructor for the S3 Manager.
	 * @param awsCredentials The AWS credentials.
	 */
	public S3Manager(AWSCredentials awsCredentials) {
		this.awsCredentials = awsCredentials;		
	}
	
	/**
	 * Constructor for the S3 Manager with a specified bucket.
	 * @param awsCredentials The AWS credentials.
	 * @param bucketname The bucket name.
	 */
	public S3Manager(AWSCredentials awsCredentials, String bucketname) {
		this.awsCredentials = awsCredentials;		
		this.bucketname = bucketname;
	}

    /**
     * Lists all the available S3 buckets.
     * @return S3 Bucket array.
     * @throws S3ServiceException Thrown when the S3Service does not work.
     */
    public S3Bucket[] listBuckets() throws S3ServiceException {
        // Provide AWSCredentials
        s3Service = new RestS3Service(awsCredentials);

        // List all your buckets.
        return s3Service.listAllBuckets();
    }

    public S3Object[] listObjects(String bucket) throws S3ServiceException {
        if(s3Service == null) {
            s3Service = new RestS3Service(awsCredentials);
        }
        return s3Service.listObjects(bucket);
    }

	/**
	 * Returns the S3Bucket specified by the name.
	 * @param name The bucket name.
	 * 
	 * @return The specified S3Bucket object.
	 * @throws S3ServiceException 
	 */
	public S3Bucket getBucket(String name) throws S3ServiceException{
		// Provide AWSCredentials
        s3Service = new RestS3Service(awsCredentials);

        // The bucket containing the dataset
        return s3Service.getBucket(name);
	}
	
	/**
	 * Returns the default bucket as specified in the Properties file.
	 * @return S3Bucket
	 * @throws S3ServiceException
	 */
	public S3Bucket getDefaultBucket() throws S3ServiceException{
		if(bucketname != null){
			return this.getBucket(bucketname);
		} else {
			System.out.println("No default bucket defined!");
			return null;
		}
	}
	
	/**
	 * Upload file to specified S3 Bucket.
	 * @param bucket The S3Bucket object.
	 * @param filename The file name.
	 * @throws S3ServiceException
	 * @throws IOException 
	 * @throws NoSuchAlgorithmException 
	 */
	public void upload(S3Bucket bucket, String filename) throws S3ServiceException, NoSuchAlgorithmException, IOException{
		// Specifiy the file to upload        
        S3Object dataObject = new S3Object(new File(filename));
        
        // Upload the object to our test bucket in S3.
        dataObject = s3Service.putObject(bucket, dataObject);
	}	

}
